<template>
	<view class="page">
		<template v-if="scrollTop<=450">
			<view class="backBox" :style="{top:menuTop+'px'}">
				<view class="Vr" style="padding: 10rpx;border-radius: 100%;">
					<u-icon name='arrow-left' color='#fff' @click='$utils.back()' size='30rpx' />
				</view>
			</view>
			<view @click="changeimage" v-if="myUser.id==wish.user_id&&!loading"
				:style="{top:menuTop+'px',right:menuRight+20+'px',height:menuHeight+'px'}" class="VrBox">
				<view class="Vr" style="border-radius: 100%;padding: 10rpx;">
					<text class="icon-tupian iconfont" style="margin-right: 0;" />
				</view>
			</view>
		</template>
		<u-navbar v-else title="心愿" border @leftClick="$utils.back()" placeholder>
		</u-navbar>

		<view style="flex: 1;">
			<z-paging ref="paging" hide-empty-view='true' style="height: 100%;" :fixed="false" v-model="List"
				@query="query" :default-page-no="1" :default-page-size="10" :auto="false" @scroll='scroll'>
				<view class="background">
					<image mode="aspectFill" style="z-index:1;background: transparent;" :src="wish.thumb"
						class="masking"></image>
					<view class="masking" v-if="!loading" />
					<view class="userBox" :style="{top:menuTop+80+'px'}">
						<u-avatar mode='aspectFill' :src='user.avatar' size='100rpx' />
						<view style="width: 20rpx;"></view>
						<view style="flex: 1;">
							<u-text :lines='1' :text='user.nick_name' color='#fff' />
							<view style="height: 20rpx;"></view>
						</view>
					</view>

					<view class="Followed viewWidth" v-if="!loading">
						<view @click.stop="Followed" class="Vr ">
							<u-icon v-if="!isFollowed" color='#fff' labelColor='#fff' name="heart" size="24rpx"
								labelSize='28rpx' label='关注'></u-icon>
							<u-icon v-else color='#fff' labelColor='#fff' name="heart-fill" size="24rpx"
								labelSize='28rpx' label='取消关注'></u-icon>
						</view>
						<view @click.stop="Thumbsup" class="Vr ">
							<u-icon v-if="!isThumbsuped" color='#fff' labelColor='#fff' name="thumb-up" size="24rpx"
								labelSize='28rpx' label='点赞'></u-icon>
							<u-icon v-else color='#fff' labelColor='#fff' name="thumb-up-fill" size="24rpx"
								labelSize='28rpx' label='取消点赞'></u-icon>
						</view>
						<view class="calendar " v-if="wish.status==-1" style="
						    right: 20px;
						    left: auto;
						    display: flex;
						    color: #fff;
						    align-items: center;">
							还剩
							<text class="Vr">
								{{wish.remain_point}}
							</text>
							积分
						</view>
					</view>
					<view class="radius" />
				</view>
				<view class="viewWidth InfoBox">
					<view class="title">
						<u-icon name="attach" size="40rpx" labelSize='24rpx' label='目标信息'></u-icon>
					</view>
					<view class="infoContent" v-if='loading'>
						<view style="width: 50%;">
							<view class="mb20">
								<view style="height: 30rpx;width: 80%;" class="Skeleton" />
							</view>
							<view class="mb20">
								<view style="height: 30rpx;width: 80%;" class="Skeleton" />
							</view>
						</view>
						<view style="width: 50%;">
							<view class="mb20">
								<view style="height: 30rpx;width: 80%;" class="Skeleton" />
							</view>
							<view class="mb20">
								<view style="height: 30rpx;width: 80%;" class="Skeleton" />
							</view>
						</view>
					</view>
					<view v-else>
						<view class="infoContent ">
							<view style="width: 50%;">
								<view class="mb20">
									<u--text lines='1' size='24rpx' :text='`开始日期：${wish.start_at}`' />
								</view>
								<view class="mb20" v-if="wish.status==-1">
									<u--text lines='1' size='24rpx' :text='`积分：${wish.point}`' />
								</view>
								<view class="mb20" v-else>
									<u--text lines='1' size='24rpx' :text='`历时：${wish.days+1}天`' />
								</view>
								<view class="mb20" v-if="wish.status==1">
									<u--text lines='1' size='24rpx' :text='`状态：已完成`' />
								</view>
							</view>
							<view style="width: 50%;">
								<view class="mb20">
									<u--text lines='1' size='24rpx' :text='`心愿：${wish.title}`' />
								</view>
								<view class="mb20" v-if="wish.status==-1">
									<u--text lines='1' size='24rpx' :text='`剩下：${wish.remain_point}积分`' />
								</view>
								<view class="mb20" v-else>
									<u--text lines='1' size='24rpx' :text='`积分：${wish.point}分`' />
								</view>
							</view>
						</view>
						
					</view>

				</view>
				<view class="viewWidth InfoBox" v-if="users.length">
					<view class="title">
						<u-icon name="account" size="40rpx" labelSize='24rpx' label='Ta的鼓励'></u-icon>
					</view>
					<view style="display:flex;overflow: auto;">
						<view v-for="(item,index) in users" :key="index" class="access">
							<u-avatar mode='aspectFill' :src="item.avatar" size='100rpx'></u-avatar mode='aspectFill'>
							<u--text lines='1' :text='item.nick_name' size='24rpx' align='center' />
						</view>
					</view>
				</view>
				<view class=" viewWidth InfoBox" >

					<view class="title">
						<u-icon name="photo" size="40rpx" labelSize='24rpx' label='动态'></u-icon>
					</view>
					<active :list='List' :loading='Listloading' ref='active' :playIndex='playIndex'
						@reload='$refs.paging.reload()' @ListloadingChange='Listloading!=Listloading' />
				</view>

			</z-paging>
		</view>
		<view class="bottom">
			<view @click="jump" v-if="wish.status==-1">
				<u-icon size='60rpx' name='camera' color='#fff' />
			</view>

			<view style="margin-top: 20rpx;" @click="commentShow=true">
				<u-icon size='60rpx' name='chat' color='#fff' />
			</view>
		</view>

		<root-portal>
			<comment :show='commentShow' @show_fun='commentShow=!commentShow' :commentId='wish_id'
				@getUsers='getUsers' />
		</root-portal>
	</view>
</template>

<script>
	import active from './components/active'
	import comment from './components/comment'
	export default {
		components: {
			active,
			comment
		},
		data() {
			return {
				Listloading:true,
				commentShow: false,
				myUser: uni.getStorageSync('user'),
				isThumbsuped: false,
				users: [],
				menuRight: 0,
				isFollowed: false,
				user_id: null,
				playIndex: 0,
				List: [],
				user: {},
				loading: true,
				wish_id: 0,
				menuTop: 0,
				scrollTop: 0,
				user: {
					profile: {}
				},
				wish: {},
				token: uni.getStorageSync('token'),

			}
		},
		onLoad(op) {
			this.wish_id = op.id
			this.getDateil()
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect() //可以获取小程序胶囊的位置。
			this.menuTop = menuButtonInfo.top
			this.menuRight = menuButtonInfo.width
			let res = uni.getSystemInfoSync()
		},
		methods: {
			changeimage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						res.tempFilePaths.forEach((item, index) => {
							uni.uploadFile({
								url: this.$config.url +
									'/api/wish/image/upload', //仅为示例，非真实的接口地址
								filePath: item,
								name: 'file',
								formData: {
									'wish_id': this.wish_id
								},
								header: {
									'Content-Type': 'application/json;charset=UTF-8',
									'Authorization': "Bearer " + uni.getStorageSync('token')
								},
								success: (res) => {
									this.wish.thumb = item
								},
							});
						})

					},
					fail: () => {

					}
				})
			},
			Thumbsup() {
				if (!this.token) {
					setTimeout(() => {
						this.$utils.judgeToken()
					}, 500)
					uni.$u.toast('未登录请先登录')
					return
				}
				if (this.isThumbsuped) {
					uni.$u.toast('取消点赞')
				} else {
					uni.$u.toast('点赞成功')
				}
				this.isThumbsuped = !this.isThumbsuped
				this.$api.Thumbsup.Thumbsup('wish', this.wish_id)

			},
			jump() {
				this.$utils.navigateTo({
					url: '/wish/active/add?id=' + this.wish_id
				})
			},
			getTimeDifferenceInDays(timestamp1, timestamp2) {
				// 将时间戳转换为日期对象
				const date1 = new Date(timestamp1);
				const date2 = new Date(timestamp2);

				// 计算两个日期之间的毫秒差
				const timeDifferenceInMilliseconds = Math.abs(date2 - date1);

				// 将毫秒转换为天数，一天有 24 * 60 * 60 * 1000 毫秒
				const oneDayInMilliseconds = 24 * 60 * 60 * 1000;
				const differenceInDays = Math.ceil(timeDifferenceInMilliseconds / oneDayInMilliseconds);

				return differenceInDays;
			},
			Followed() {
				if (!this.token) {
					setTimeout(() => {
						this.$utils.judgeToken()
					}, 500)
					uni.$u.toast('未登录请先登录')
					return
				}
				if (this.isFollowed) {
					uni.$u.toast('取消关注')
					this.$api.Followed.DelFollow({
						user_id: this.user.id
					}).then(res => {}).catch(err => {
						uni.$u.toast(err.data.message)
						this.isFollowed = !this.isFollowed
					})
				} else {
					uni.$u.toast('关注成功')
					this.$api.Followed.Follow({
						user_id: this.user.id
					}).then(res => {}).catch(err => {
						uni.$u.toast(err.data.message)
						this.isFollowed = !this.isFollowed
					})
				}
				this.isFollowed = !this.isFollowed

			},
			getUsers(e) {
				const users = []
				for (let i in e) {
					users.push(e[i])
				}
				this.users = users
				console.log(this.users)
			},
			getDateil() {
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.Wish.Detail(this.wish_id).then(res => {
					this.wish = res.data.wish
					this.user = res.data.wish.user
					this.isFollowed = res.data.isFollowed
					this.isThumbsuped = res.data.isThumbsuped
					this.loading = false
					this.query(1, 10)
				}).catch(err => {
					uni.hideLoading()
				})

			},
			scroll(e) {
				const top = e.detail.scrollTop
				this.scrollTop = top * 2
				let index = 0
				let domTop0 = 0
				const query = uni.createSelectorQuery().in(this.$refs.active);
				query.selectAll('.activeBox').boundingClientRect(data => {
					data.forEach((rect, domIndex) => {
						let domTop = rect.top + top
						if (domIndex == 0) {
							domTop0 = domTop
						}
						if (top > (domTop - domTop0)) {
							index = domIndex
						}
						if (domIndex == data.length - 1) {
							this.playIndex = index

						}
					});
				}).exec();
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.Wish.Active.List(this.wish_id, obj).then(res => {
					this.Listloading = false
					this.$refs.paging.complete(res.data.fragments.data.map(item => {
						return {
							...item,
							user: this.user
						}
					}))
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
				})


			}

		}
	}
</script>

<style lang="scss" scoped>
	.Followed {
		margin-bottom: 0;
		z-index: 111;
		position: absolute;
		left: 40rpx;
		bottom: 40rpx;
		display: flex;
		align-items: center;

		>view {
			margin-right: 10rpx;
			border-radius: 100rpx;
		}
	}

	.access {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 26rpx;
		margin-right: 20rpx;
		text-align: center;
		margin-top: 20rpx;
	}

	.InfoBox {
		margin: 20rpx auto;
		background-color: #fff;
		box-shadow: 0 0 10rpx #ddd;
		padding: 10rpx;
		border-radius: 8rpx;

		.title {
			width: 100%;
			border-bottom: 1rpx solid #ddd;
			margin: 0 0 20rpx;
		}

		.infoContent {
			display: flex;
			width: 100%;
			justify-content: space-between;
		}
	}

	.selectSwitchBox {
		margin: 20rpx auto;
	}

	.calendar {
		position: absolute !important;
		left: 40rpx;
		z-index: 11;
		padding: 10rpx 20rpx !important;
	}

	.page {
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		background-color: #f5f5f5;
		display: flex;
		flex-direction: column;
	}

	.stepsBox {
		padding: 0rpx 20rpx;
		height: 100rpx;
		position: absolute;
		z-index: 1;
		display: flex;
		overflow: auto;
	}

	.fff {
		background-color: #f5f5f5;
		width: 100%;
		z-index: 111;
	}

	.radius {
		border-radius: 32rpx 32rpx 0 0;
		height: 20rpx;
		position: absolute;
		bottom: 0;
		width: 100vw;
		background-color: #f5f5f5;

	}



	.background {
		height: 500rpx;
		width: 100%;
		position: relative;
	}

	.masking {
		background: linear-gradient(to top, rgba(0, 0, 0, 0.3), rgba(255, 255, 255, 0), rgba(255, 255, 255, 0), rgba(255, 255, 255, 0), rgba(255, 255, 255, 0));
		position: absolute;
		width: 100%;
		height: 500rpx;
		z-index: 2;
	}

	.backBox {
		position: fixed;
		left: 40rpx;
		z-index: 1111111111;
	}

	.Vr {
		background: rgba(0, 0, 0, 0.3);
		padding: 10rpx 20rpx;
		border-radius: 100rpx;
		color: #fff;
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;

		>button {
			opacity: 0;
			position: absolute;
			z-index: 1;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
		}
	}



	.iconfont {
		margin-right: 10rpx;
	}

	.userBox {
		display: flex;
		position: absolute;
		width: 90vw;
		left: 40rpx;
		z-index: 111;
	}

	.radius {
		position: absolute;
		bottom: 0;
		height: 20rpx;
		width: 100vw;
		background-color: #f5f5f5;
		border-radius: 16rpx 16rpx 0 0;
		z-index: 11;
	}

	.bottom {
		position: fixed;
		bottom: 10vh;
		right: 5vw;
		z-index: 1;

		>view {
			display: flex;
			width: 80rpx;
			height: 80rpx;
			background-color: #999;
			border-radius: 16rpx 16rpx 0 0;
			align-items: center;
			justify-content: center;
			border-radius: 100%;
		}
	}

	.access {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 26rpx;
		text-align: center;
		margin-top: 20rpx;
		margin-right: 10rpx;
	}

	.iconBox {
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 8rpx 4rpx;
	}

	.VrBox {
		display: flex;
		position: absolute;
		align-items: center;
		z-index: 111;

		>view {
			margin-right: 10rpx;
		}
	}


</style>